# coding: utf-8

import subprocess

class ExternalGzip(object):
    def __init__(self,):
        pass
    def open(self, filename, mode='r'):
        if mode == 'r':
            p = subprocess.Popen(
                args = 'zcat %s' % filename,
                shell = True,
                stdout = subprocess.PIPE,
            )
            return p.stdout
        elif mode == 'w':
            p = subprocess.Popen(
                args = 'gzip > %s' % filename,
                shell = True,
                stdin = subprocess.PIPE,
            )
            return p.stdin

        else:
            raise Exception("Unknow gzip mode: %s" % mode)


try:
    import gzip
except ImportError:
    gzip = ExternalGzip()
 
